package com.erp.bus.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.bus.Util.WebUtil;
import com.erp.bus.comm.Code;
import com.erp.bus.comm.DataAndView;
import com.erp.bus.comm.ResultCode;
import com.erp.bus.pojo.Goods;
import com.erp.bus.pojo.Inport;
import com.erp.bus.pojo.Provider;
import com.erp.bus.service.GoodsService;
import com.erp.bus.service.InportService;
import com.erp.bus.service.ProviderService;
import com.erp.bus.vo.InportVo;
import com.erp.sys.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("inport")
public class InportController {

    @Autowired
    private InportService inportService;

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ProviderService providerService;



    //http://localhost:8080/inport/loadAllInport?page=1&limit=10
    //http://localhost:8080/inport/loadAllInport?page=1&limit=10&providerid=0&goodsid=0&startTime=&operateperson=&remark=&endTime=
    @RequestMapping("loadAllInport")
    public DataAndView loadAllInport(InportVo inportVo){

        System.out.println(inportVo.getStartTime());
        IPage<Inport> page = new Page<Inport>(inportVo.getPage(),inportVo.getLimit());

        QueryWrapper<Inport> qw = new QueryWrapper<>();
        // 如果条件不为空,就添加
        if( inportVo.getProviderid()!=null && inportVo.getProviderid()!=0){
            qw.like("providerid",inportVo.getProviderid());
        }
        if( inportVo.getGoodsid()!=null && inportVo.getGoodsid()!=0){
            qw.like("goodsid",inportVo.getGoodsid());
        }

        //把String  转化为  Date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = null;
        Date endTime = null;

        if(inportVo.getStartTime()!=null && inportVo.getStartTime()!= ""){
            try{
                startTime = sdf.parse(inportVo.getStartTime());
                System.out.println(startTime);
            }catch (Exception e){
            }
            qw.gt("inporttime",new Timestamp(startTime.getTime()));
        }
        if(inportVo.getEndTime()!=null && inportVo.getEndTime() != ""){
            try{
                endTime =  sdf.parse(inportVo.getEndTime());

                System.out.println();
            }catch (Exception e){
            }
            qw.lt("inporttime",new Timestamp(endTime.getTime()));
        }

        if (inportVo.getOperateperson() != null){
            qw.like("operateperson",inportVo.getOperateperson());
        }
        if(inportVo.getRemark()!=null){
            qw.like("remark",inportVo.getRemark());
        }
        // 查询
        inportService.page(page,qw);
        List<Inport> list = page.getRecords();
        List<Inport> list2 = new ArrayList<>();
        for(Inport inport:list){
            // 获取当前商品的供货商id
            Integer provideId = inport.getProviderid();
            //通过供货商id 查对应的供货商
            Provider provider = providerService.getById(provideId);
            // 设置当前的商品的供货商名称
            inport.setProvidername(provider.getProvidername());

           Integer goodsId =  inport.getGoodsid();
           Goods good = goodsService.getById(goodsId);
           // 设置商品名称
           inport.setGoodsname(good.getGoodsname());
           //设置商品规格
            inport.setSize(good.getSize());

            list2.add(inport);
        }

        return new  DataAndView(list2);

    }

    //http://localhost:8080/inport/addInport
    @RequestMapping("addInport")
    public ResultCode addInport(Inport inport){

        inport.setId(null);

        // 设置操作员信息
       HttpSession session =  WebUtil.getSession();
       User user = (User) session.getAttribute("user");
       inport.setOperateperson(user.getName());
       inport.setInporttime(new Date());
       boolean bool =  inportService.save(inport);
        if(bool){
            return ResultCode.SUCCESS_ADD;
        }else{
            return ResultCode.FAIL_ADD;
        }
    }


    // url   http://localhost:8080/inport/updateInport

    @RequestMapping("updateInport")
    public ResultCode updateInport(Inport inport){
        ;try{
            boolean bool = inportService.updateById(inport);
            if(bool){
                return ResultCode.SUCCESS_UPDATE;
            }else{
                return ResultCode.FAIL_UPDATE;
            }
        }catch (Exception e){

            return  ResultCode.FAIL_ADD;
        }

    }

    //  http://localhost:8080/inport/deleteInport
    @RequestMapping("deleteInport")
    public ResultCode  deleteInport(Inport id){

        ;try{
            boolean bool = inportService.removeById(id);
            System.out.println(bool);
            if(bool){
                return ResultCode.SUCCESS_DELETE;
            }else{
                return  ResultCode.FAIL_DELETE;
            }
        }catch (Exception e){

            return  ResultCode.FAIL_DELETE;
        }
    }
}
